﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using RockCompany.Master.DTO;
using RockCompany.Master.Model;
using System.Data;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using System.Windows.Forms;

namespace RockCompany.Master.Validator
{
    public class UserValidator
    {
        public static bool IsValidUserName(string userName)
        {
            Regex sUserNameAllowedRegEx = new Regex(@"^[a-zA-Z][a-zA-Z0-9\._\-]{0,28}?[a-zA-Z0-9]{0,2}$", RegexOptions.Compiled);
            
            return !string.IsNullOrEmpty(userName) && sUserNameAllowedRegEx.IsMatch(userName.Trim());
        }

        public static bool IsEmpty(string strval)
        {
            return string.IsNullOrEmpty(strval);
        }

        public bool ExistsUserName(string userName)
        {
            UserDTO userDTO = new UserDTO();
            userDTO.username = userName;
            userDTO.groupName = "";
            SC03FormModel modelSC03 = new SC03FormModel();
            DataTable resultDT = modelSC03.GetUsers(userDTO);
            return CommonModel.existData(resultDT); // true if user name exists
        }

        public static string isValidNumer(object value)
        {
            string sEr = string.Empty;
            if (!string.IsNullOrEmpty(value.ToString()))
            {
                try
                {
                    decimal d = decimal.Parse(value.ToString());
                    if (d <= -1 || d < 0)
                        sEr = CommonConstantIF.NumberValueError;
                }
                catch (Exception)
                {
                    sEr = CommonConstantIF.NumberFormateError;
                }
            }
            return sEr;
        }

        public static void validateNumberOfCell(Form owner, Translator trans, DataGridViewCellValidatingEventArgs e)
        {
            string s = isValidNumer(e.FormattedValue);
            e.Cancel = !string.IsNullOrEmpty(s);
            if (e.Cancel)
            {
                InforDialog.showWarningrBox(owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }
    }
}
